//
//  SecondDiaryViewController.m
//  土巴兔
//
//  Created by 千锋 on 15/12/31.
//  Copyright (c) 2015年 IOS1503薛凯. All rights reserved.
//

#import "SecondDiaryViewController.h"
#import "AFNetworking.h"
#import "Default.h"
#import "DiarySecondModel.h"
#import "DiarySecondTableViewCell.h"
@interface SecondDiaryViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;

@property (strong,nonatomic)UITableView* tableview;
@property (strong,nonatomic)NSMutableArray* dataArray;
@end

@implementation SecondDiaryViewController

- (void)viewDidLoad {
    [super viewDidLoad]; 
    _segment.layer.cornerRadius=16;
    _segment.clipsToBounds=YES;
    // Do any additional setup after loading the view from its nib.
    _segment.selectedSegmentIndex=1;
    self.navigationController.navigationBarHidden=YES;
    [self getData];
    [self createTableView];
    
}
- (IBAction)PressSegment:(UISegmentedControl *)sender {
    [self.navigationController popViewControllerAnimated:NO];
}
#pragma mark- 获取数据
- (void)getData{
    _dataArray=[[NSMutableArray alloc] initWithCapacity:0];
    AFHTTPRequestOperationManager* manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSString* urlStr=@"http://mobileapi.to8to.com/index.php";
    NSMutableDictionary* paras=[NSMutableDictionary dictionaryWithDictionary:@{@"action":@"list",@"perPage":@"20",@"appid":@"15",@"to8to_token":@"5577450_75ff93ea5d8fae3c8a5a0ef1cb6dc1db",@"systemversion":@"22",@"imei":@"569eb1056c38e1e8",@"appversion":@"3.6.0",@"appostype":@"1",@"version":@"2.5",@"uid":@"5577450",@"type":@"1",@"page":@"1",@"channel":@"%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83",@"paging":@"1",@"model":@"live"}];
    [paras setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"to8to_token"] forKey:@"to8to_token"];
    [paras setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"] forKey:@"uid"];
//    =&=&=&=&=&=&=&=&=&=&=&=&=&=&=&
    [manager POST:urlStr parameters:paras success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray* arr=[dic objectForKey:@"data"];
        for (NSDictionary* item in arr) {
            DiarySecondModel* model=[[DiarySecondModel alloc] initWithDictionary:item];
            for (NSDictionary* item2 in [item objectForKey:@"style"]) {
                [model.style addObject:[item2 objectForKey:@"value"]];
            }
            for (NSString* item3 in [item objectForKey:@"diary_image"]) {
                [model.diary_image addObject:item3];
            }
            [_dataArray addObject:model];
            [_tableview reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
#pragma mark- 创建表格
- (void)createTableView{
    _tableview=[[UITableView alloc] initWithFrame:CGRectMake(0, 55, DEVICEWIDTH, DEVICEHEIGHT)];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    [self.view addSubview:_tableview];
    [_tableview registerClass:[DiarySecondTableViewCell class] forCellReuseIdentifier:@"DiarySecondTableViewCell"];
}
#pragma mark- 表格协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DiarySecondTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"DiarySecondTableViewCell"];
    cell.backgroundColor=[UIColor colorWithRed:192.0/255 green:192.0/255 blue:192.0/255 alpha:1];
    [cell relayoutWithModel:_dataArray[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 250;
}
@end
